//
//  MusicSecTableViewCell.m
//  A_program
//
//  Created by 杨利佳 on 15/5/22.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "MusicSecTableViewCell.h"
#import "MusicSecButton.h"
#import "NovelDatabase.h"
#import "NovelModel.h"

@implementation MusicSecTableViewCell

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.colButton = [[MusicSecButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 5 * 4, 100, [UIScreen mainScreen].bounds.size.width / 5 - 10, 30)];
    self.colButton.layer.borderColor = [UIColor orangeColor].CGColor;
    self.colButton.layer.borderWidth = 1.0f;
    self.colButton.layer.cornerRadius = 5.0f;
    self.colButton.layer.masksToBounds = YES;
    self.colButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.colButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self addSubview:_colButton];
    [_colButton release];
    [_colButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [_colButton addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
    
    self.image1.image = [UIImage imageNamed:@"yy.png"];
    self.image2.image = [UIImage imageNamed:@"play1.png"];
    self.image3.image = [UIImage imageNamed:@"tra.png"];
}


// 收藏按钮
- (void)collect:(id)sender
{
#pragma mark-
#pragma mark 判断是否已收藏
    NSUserDefaults *myLoginName = [NSUserDefaults standardUserDefaults];
    NSInteger myAlbumId = [self.colButton.collectId integerValue];
    NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:myAlbumId loginName:[myLoginName objectForKey:@"loginName"]];
    if ([array count] == 0 && [self.colButton.titleLabel.text isEqualToString:@"收藏"]) {
        
        [self.collectDelegate createCollectAction:self.colButton.collectId];
        if (![[myLoginName objectForKey:@"loginName"] length] == 0 &&[[myLoginName objectForKey:@"state"]isEqualToString:@"登录"]) {
            [self.colButton setTitle:@"已收藏" forState:UIControlStateNormal];
        }else{
            NSLog(@"未登录或则没有用户");
            
            
        }

        
    }else if([self.colButton.titleLabel.text isEqualToString:@"已收藏"]){
                NovelModel *myid = [[NovelModel alloc]init];
        for (NovelModel *model in array) {
            myid = model;
        }
        [NovelDatabase deleteWithModel:myid];
        [self.colButton setTitle:@"收藏" forState:UIControlStateNormal];

        
    }else{
        NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:[self.colButton.collectId integerValue] loginName:[myLoginName objectForKey:@"loginName"]];
        if ( ![array count] == 0) {
            [self.colButton setTitle:@"已收藏" forState:UIControlStateNormal];
        }
    }
 

    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

- (void)dealloc {
    
    [_showImage release];
    [_title release];
    [_playCounts release];
    [_tracksCounts release];
    [_image1 release];
    [_image2 release];
    [_image3 release];
    [_colButton release];
    [super dealloc];
}


@end
